
/**
 * AppleIIGo
 * Mouse class
 * (C) 2008 by Marc S. Ressl (mressl@umich.edu)
 * Released under the GPL
 */

#include "Mouse.h"

#define saturate(value, low, high)	value = ((value) < (low) ? (low) : \
									(value) > (high) ? (high) : \
									(value))

Mouse::Mouse(Motherboard * motherboard) : Device(motherboard, "Mouse") {
	initButton(0);
	initButton(1);
	initButton(2);
	
	mouseInterrupt = new Interrupt(motherboard, "MouseInterrupt");
}

void Mouse::onResetAsserted(bool isRestart) {
	if (!isRestart)
		return;
	
	paddleXPosition = PADDLEPOSITION_CENTER;
	paddleYPosition = PADDLEPOSITION_CENTER;
	
	enableXYInterrupts(false);
}

void Mouse::moveMouse(SINT32 relativeX, SINT32 relativeY) {
	paddleXPosition += relativeX;
	paddleYPosition += relativeY;
	saturate(paddleXPosition, PADDLEPOSITION_LOW, PADDLEPOSITION_HIGH);
	saturate(paddleYPosition, PADDLEPOSITION_LOW, PADDLEPOSITION_HIGH);
	motherboard->getPaddles()->setPosition(0, paddleXPosition);
	motherboard->getPaddles()->setPosition(1, paddleYPosition);
}

void Mouse::initButton(UINT32 index) {
	buttonCount[index] = 0;
}

void Mouse::pushButton(UINT32 index) {
	if (index < MOUSEBUTTON_NUM) {
		buttonCount[index]++;
		motherboard->getPaddles()->pushButton(index);
	}
}

void Mouse::pullButton(UINT32 index) {
	if (index < MOUSEBUTTON_NUM) {
		buttonCount[index]--;
		motherboard->getPaddles()->pullButton(index);
	}
}

bool Mouse::isButtonDown(UINT32 index) {
	if (index < MOUSEBUTTON_NUM)
		return buttonCount[index];
	
	return false;
}

void Mouse::enableXYInterrupts(bool value) {
}

bool Mouse::isXYInterruptsEnabled() {
	return false;
}

bool Mouse::clearXInterrupt() {
	return false;
}

bool Mouse::clearYInterrupt() {
	return false;
}

void Mouse::setXInterruptOnFallingEdge(bool value) {
}

bool Mouse::isXInterruptOnFallingEdge() {
	return false;
}

void Mouse::setYInterruptOnFallingEdge(bool value) {
}

bool Mouse::isYInterruptOnFallingEdge() {
	return false;
}

bool Mouse::isXDirectionHigh() {
	return false;
}

bool Mouse::isYDirectionHigh() {
	return false;
}

void Mouse::setInterruptRegister(UINT8 value) {
	interruptRegister = value;
}

UINT8 Mouse::getInterruptRegister() {
	return interruptRegister;
}

UINT8 Mouse::getInterruptFlagRegister() {
	return interruptFlagRegister;
}

UINT8 Mouse::getDeltaXRegister() {
	return 0;
}

UINT8 Mouse::getDeltaYRegister() {
	return 0;
}

void Mouse::clearXYInterruptRegister() {
}
